﻿using DFGK.Service.BBS;
using DFGK_WMS.Model.BBS;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Model.Models.BBS;
using DFGK_WMS.Model.Models.LyModel;
using DFGK_WMS.Model.Wcs;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace DFGK_WMS.Context
{
    public class DFGK_DB : DbContext
    {
        public DFGK_DB(DbContextOptions<DFGK_DB> options) : base(options) { }

        public DbSet<Model.Models.Action> Actions { get; set; }

        public DbSet<Area> Areas { get; set; }

        public DbSet<BarCodeRule> BarCodeRules { get; set; }

        public DbSet<BindPickingPosition> BindPickingPositions { get; set; }

        public DbSet<Cell> Cells { get; set; }

        public DbSet<Customer> Customers { get; set; }

        public DbSet<Department> Departments { get; set; }

        public DbSet<Device> Devices { get; set; }

        public DbSet<DictItem> DictItems { get; set; }

        public DbSet<DictType> DictTypes { get; set; }

        public DbSet<ErrorLog> ErrorLogs { get; set; }

        public DbSet<FacilityTwin> FacilitiyTwins { get; set; }

        public DbSet<Instruct> Instructs { get; set; }

        public DbSet<InterfaceLog> InterfaceLogs { get; set; }

        public DbSet<Inventory> Inventorys { get; set; }

        public DbSet<InventoryLog> InventoryLogs { get; set; }

        public DbSet<InventoryAdjust> InventoryAdjusts { get; set; }

        public DbSet<InventoryDetail> InventoryDetails { get; set; }

        public DbSet<Log> Logs { get; set; }

        public DbSet<Material> Materials { get; set; }

        public DbSet<MaterialCategory> MaterialCategories { get; set; }

        public DbSet<MaterialGroup> MaterialGroups { get; set; }

        public DbSet<MaterialKey> MaterialKeys { get; set; }

        public DbSet<Menu> Menus { get; set; }

        public DbSet<MenuAction> MenuActions { get; set; }

        public DbSet<Pallet> Pallets { get; set; }

        public DbSet<PickingOrder> PickingOrders { get; set; }

        public DbSet<PickingOrderDetail> PickingOrdersDetails { get; set; }

        public DbSet<PickingPosition> PickingPositions { get; set; }

        public DbSet<PutawayOrder> PutawayOrders { get; set; }

        public DbSet<PutawayOrderDetail> PutawayOrdersDetails { get; set; }

        public DbSet<ReceivingBill> ReceivingBills { get; set; }

        public DbSet<ReceivingBillDetail> ReceivingBillsDetails { get; set; }

        public DbSet<ReceivingOrderDetail> ReceivingOrderDetails { get; set; }

        public DbSet<Role> Roles { get; set; }

        public DbSet<RoleMenuAction> RoleMenuActions { get; set; }

        public DbSet<Routeway> Routeways { get; set; }

        public DbSet<Shelf> Shelfs { get; set; }

        public DbSet<ShippingOrderDetail> ShippingOrdersDetails { get; set; }

        public DbSet<ShippingLockCell> ShippingLockCells { get; set; }

        public DbSet<StackingStorage> StackingStorages { get; set; }

        public DbSet<Station> States { get; set; }

        public DbSet<StocktakeResultRecord> StocktakeResultRecords { get; set; }

        public DbSet<StocktakingOrder> StocktakingOrders { get; set; }

        public DbSet<StocktakingOrderDetail> StocktakingOrdersDetails { get; set; }

        public DbSet<Supplier> Suppliers { get; set; }

        public DbSet<TransferBinOrder> TransferBinOrders { get; set; }

        public DbSet<TransferBinOrderDetail> TransferBinOrdersDetails { get; set; }

        public DbSet<User> Users { get; set; }

        public DbSet<UserRole> UserRoles { get; set; }

        public DbSet<Warehouse> WarehouseUsers { get; set; }

        public DbSet<ScrapOrder> ScrapOrder { get; set; }

        public DbSet<ScrapOrderDetail> ScrapOrderDetail { get; set; }




        public DbSet<EquipmentAllocate> EquipmentAllocates { get; set; }

        public DbSet<EquipmentUnitsAllocate> EquipmentUnitsAllocates { get; set; }

        #region BBS
        public DbSet<BBS_Post> BBS_Post { get; set; }
        public DbSet<BBS_Area> BBS_Area { get; set; }
        #endregion

        #region ShareFile 
        public DbSet<ShareFileTypeInfo> ShareFileTypes { get; set; }
        public DbSet<ShareFileInfo> ShareFileInfo { get; set; }
        #endregion


        #region  Infomation
        public DbSet<InfomationType> InfomationTypes { get; set; }

        public DbSet<Infomation> Infomations { get; set; }
        #endregion

        public DbSet<WorkFlow> WorkFlows { get; set; }
        public DbSet<MaterilaInfo> MaterialInfos { get; set; }

        public DbSet<EquipmentBasic> EquipmentBasics { get; set; }

        public DbSet<EquipmentBad> EquipmentBads { get; set; }

        public DbSet<MatchingEquipmentDetail> MatchingEquipmentDetails { get; set; }
        #region  LYDB
        public DbSet<Equipment> Equipments { get; set; }
        public DbSet<EquipmentChange> EquipmentChanges { get; set; }
        public DbSet<EquipmentStatistics> EquipmentStatistics { get; set; }
        public DbSet<UnitEquipment> UnitEquipments { get; set; }
        public DbSet<Vehicles> Vehicles { get; set; }
        public DbSet<VehiclePurchases> VehiclePurchases { get; set; }
        public DbSet<VehicleStatus> VehicleStatus { get; set; }
        public DbSet<VehicleOrganizationStats> VehicleOrganizationStats { get; set; }
        public DbSet<Region> Regions { get; set; }
        public DbSet<Organization> Organizations { get; set; }
        public DbSet<Statistics> Statisticss { get; set; }
        public DbSet<PurchaseStandard> PurchaseStandards { get; set; }
        public DbSet<Quote> Quotes { get; set; }
        public DbSet<PurchasePlan> PurchasePlans { get; set; }
        public DbSet<PurchaseOrder> PurchaseOrders { get; set; }
        public DbSet<PurchaseOrderItem> PurchaseOrderItems { get; set; }
        public DbSet<DeliveryNote> DeliveryNotes { get; set; }
        public DbSet<BidAnnouncement> BidAnnouncements { get; set; }
        public DbSet<VendorRegistration> VendorRegistrations { get; set; }
        public DbSet<ExpenseClaim> ExpenseClaims { get; set; }
        public DbSet<VendorReconciliationRequest> VendorReconciliationRequests { get; set; }
        public DbSet<SupplierReconciliation> SupplierReconciliations { get; set; }
        public DbSet<Purchase> Purchases { get; set; }
        public DbSet<SupplierQuote> SupplierQuotes { get; set; }
        public DbSet<Transaction> Transactions { get; set; }
        public DbSet<TransactionNew> TransactionNews { get; set; }
        public DbSet<SupplierTransaction> SupplierTransactions { get; set; }
        public DbSet<ProcurementCostAnalysis> ProcurementCostAnalysiss { get; set; }
        public DbSet<MaintenanceOrder> MaintenanceOrders { get; set; }
        public DbSet<ScrapLossOrder> ScrapLossOrders { get; set; }

        public DbSet<EMPModel> EMPModel { get; set; }


        public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
        public DbSet<NumberPersonnel> NumberPersonnels { get; set; }
        public DbSet<MatchingEquipment> MatchingEquipments { get; set; }
        public DbSet<InventoryInitialization> InventoryInitializations { get; set; }
        public DbSet<VendorTransaction> VendorTransactions { get; set; }
        public DbSet<MaintenanceWork> MaintenanceWorks { get; set; }


        #endregion

        #region EquipmentMaintenance
        public DbSet<EquipmentMaintenance> EquipmentMaintenances { get; set; }

        public DbSet<EquipmentBorrow> EquipmentBorrows { get; set; }

        public DbSet<EquipmentType> EquipmentTypes { get; set; }

        public DbSet<NetworkAddress> NetworkAddresss { get; set; }

        public DbSet<AdminConfiguration> AdminConfigurations { get; set; }

        public DbSet<RepairApplication> RepairApplications { get; set; }

        public DbSet<EquipmentCollection> EquipmentCollections { get; set; }

        #endregion


        public DbSet<EPCDevice> EPCDevices { get; set; }
        public DbSet<ScrappedWork> ScrappedWorks { get; set; }
        public DbSet<InventoryDay> InventoryDays { get; set; }
        public override int SaveChanges()
        {
            //如果状态为Added则为审计字段赋值
            foreach (var item in ChangeTracker.Entries<BaseEntity>())
            {
                if (item.State == EntityState.Added)
                {
                    item.Entity.Id = YitIdHelper.NextId();
                    item.Entity.CreateDate = DateTime.Now;
                }
                else if (item.State == EntityState.Modified)
                {
                    item.Entity.ModifyDate = DateTime.Now;
                    item.Property(x => x.CreateUserId).IsModified = false;
                    item.Property(x => x.CreateUserName).IsModified = false;
                    item.Property(x => x.CreateDate).IsModified = false;
                }
            }
            return base.SaveChanges();
        }
    }
}
